package com.example.administrator.testproject.MVP.Adapter;

import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.util.LruCache;
import com.example.administrator.testproject.MVP.Entity.TestDto;
import com.example.administrator.testproject.MyApplication.MyApplication;
import com.example.administrator.testproject.R;

import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2016/10/24 0024.
 */

public class TestAdapterC extends RecyclerView.Adapter<TestAdapterC.MyViewHolder>{

    private List<TestDto> mData;

    public TestAdapterC(List<TestDto> datas) {
        this.mData = datas;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder viewHolder = new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.test_list_item, parent, false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if(mData.get(position).getViewHeight() == 0) {
            int height = holder.itemView.getLayoutParams().height;
            mData.get(position).setViewHeight((int) ((Math.random() * 10 * height / 2 - height * 2) + height));
        }
        holder.itemView.getLayoutParams().height = mData.get(position).getViewHeight();
        holder.setText(R.id.textView2, mData.get(position).getTitle());
        holder.setImageResource(R.id.imngView2 , mData.get(position).getImageUrl());
    }

    @Override
    public int getItemCount() {
        return mData != null ? mData.size() : 0;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private View itemView;

        public MyViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
        }

        private View getView(int viewId){
            return itemView.findViewById(viewId);
        }

        private void setText(int viewId, String contentStr){
            ((TextView) getView(viewId)).setText(contentStr);
        }

        private void setImageResource(int viewId, String imageUrl){
            Glide.with(MyApplication.getInstance()).load(imageUrl).into((ImageView) getView(viewId));
        }
    }
}
